﻿using System;
using System.Collections.Generic;
using System.Text;
using HR60_PanelSoft.Model;
using HR60_PanelSoft.Model.BaseModel;
using HR60_PanelSoft.FileUtils;

namespace HR60_PanelSoft.Service
{
	public class ProjService : CommonService
	{
		// 当前项目文件路径
		private static string currentProjFilePath = null;

		// 创建新工程
		public static void createNew()
		{
			proj = new Proj ();
			proj.name = "新建项目";
			proj.date = DateTime.Now;
			proj.writer = "默认作者";
			proj.addr = "默认地址";
			proj.defaultStyle = UIStyle.QingShuang;
			currentProjFilePath = null;
		}

		// 保存工程
		public static void save(Proj dto)
		{
			proj.name = dto.name;
			proj.writer = dto.writer;
			proj.date = dto.date;
			proj.addr = dto.addr;
			proj.defaultStyle = dto.defaultStyle;
		}

		// 另存为文件
		public static void saveFileAs(string path)
		{
			JsonFileUtils.saveProj (proj, path);
			currentProjFilePath = path;
		}

		// 保存文件
		public static void saveFile()
		{
			JsonFileUtils.saveProj (proj, currentProjFilePath);
		}

		// 载入
		public static void load(string path)
		{
			Proj temp = new Proj ();
			temp.load (path);
			proj = temp;
			currentProjFilePath = path;
		}

		// 获得当前项目
		public static Proj getCurrentProj()
		{
			return proj;
		}

		// 获得当前保存目录
		public static string getCurrentProjFilePath()
		{
			return currentProjFilePath;
		}

		// 测试数据
		public static Proj createDemo()
		{
			DeviceCreator dc = new DeviceCreator ();
			dc.loadTypes ();
			PanelCreator pc = new PanelCreator ();
			pc.loadTypes ();

			ProjService.createNew ();

			for (int i=1; i<6; i++)
			{
				DeviceGroup group = new DeviceGroup ();
				group.name = "设备组" + i;
				DeviceGroupService.append (group);

				for (int j=0; j<3; j++) 
				{
					if (i != 1 && dc.deviceTypes[j].exType != ExDeviceType.NULL) continue;
					Device dev = dc.create (dc.deviceTypes [j]);
					DeviceService.append (dev, group);
				}
			}
			for (int i=1; i<6; i++)
			{
				HR60_PanelSoft.Model.Net net = new HR60_PanelSoft.Model.Net ();
				net.index = i;
				net.name = "网络" + i;
				NetService.append (net);

				for (int j=0; j<2; j++)
				{
					Panel pp = pc.create(pc.panelTypes[j]);
					PanelService.append (pp, net);
				}
			}
			for (int i=1; i<9; i++) 
			{
				Scene scene = new Scene ();
				scene.name = "场景" + i;
				SceneService.append (scene);
			}
			return ProjService.getCurrentProj();
		}
	}
}
